import Vue from 'vue'
import VueRouter from 'vue-router'
import URL from './util/URL.js';
import Browser from './util/Browser.js';
import api from './vuex/api';
import { Indicator } from 'mint-ui';
import { wechatLoginByCode, wechatAuth } from './util/wechat';

Vue.use(VueRouter);
const scrollBehavior = to => {
	const position = {}
	if (to.hash) {
		position.selector = to.hash
	}
	if (to.matched.some(mm => mm.meta.scrollToTop)) {
		position.x = 0
		position.y = 0
	}
	return position
}


function caschUserInfo(res){
	localStorage.access_token = res.access_token;
	localStorage.is_verified = res.client.is_verified;
	localStorage.name = res.client.name;
	localStorage.userid = res.client.userid;
	localStorage.expiresTime = res.expires_time;
	localStorage.headimgurl = res.client.headimgurl;
}

// const fairGuardRoute = (to, from, next) => {
// 	//页面打开前的认证
// 	Indicator.close();
// 	if(URL.params.code&&localStorage.code!=URL.params.code) {
// 			localStorage.code = URL.params.code;
// 			wechatLoginByCode('/v1/token/wechat/mp',URL.params.code).then((data)=>{
// 				caschUserInfo(data);
// 				if(localStorage.is_verified&&localStorage.is_verified == 0){
// 					router.push('/myIdentification');
// 				} else {
// 					next();
// 				}
// 			});
// 	} else {
// 		next();
// 	}

	// } else if(typeof(localStorage.access_token) === 'undefined'){
	// 	wechatAuth('/v1/token/wechat/mp');
	// } else {
// }

// 路由定义
const routers = [
{
  path: '/index',
  name:'index',
	meta: {title: '爱仁医生'},
	// beforeEnter: fairGuardRoute,
  component(resolve) {
      require(["./containers/home/index.vue"], resolve)
  },
	// 定义子路由
},
{ path: '/', redirect: '/index' }
];



let router = new VueRouter({
	mode: 'history',
	base: __dirname,
	scrollBehavior,
	routes: routers
})
//路由渲染前
router.beforeEach((to, from, next) => {
	if (to.meta.title) {
		document.title = to.meta.title;
	}
	next()
})

export default router
